﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace Adams.Public.Api.Controllers.Web
{
    [EnsureHttps]
    public class DonateController : Controller
    {
        public ActionResult PaymentPageLoad()
        {
            var model = DataAccess.Web.Donate.Payment.PageLoad();
            return new JsonXmlResult(model);
        }

        public ActionResult SavePayment(DataTransfer.Web.Donate.Payment.FormInput input)
        {
            var output = Validation.PerformBasicValidation(input);

            if (output.IsValid)
            {
                if (DateTime.Now.Year > input.CardExpireYear.ToInt() ||
                    (DateTime.Now.Year == input.CardExpireYear.ToInt() && DateTime.Now.Month > input.CardExpireMonth.ToInt()))
                {
                    output.InvalidFields.Add(new DataTransfer.InvalidField()
                    {
                        Name = "ExpireYear",
                        Code = 25
                    });
                }

                if (Shared.Validation.Currency(input.Amount))
                {
                    if (input.Amount.ToDecimal() > 2500)
                    {
                        output.InvalidFields.Add(new DataTransfer.InvalidField()
                        {
                            Name = "Amount",
                            Code = 27
                        });
                    } 
                } 
                else
                {
                    output.InvalidFields.Add(new DataTransfer.InvalidField()
                    {
                        Name = "Amount",
                        Code = 1
                    });
                }

                if (output.IsValid)
                {
                    var save = Shared.ObjectHelper.CopyObject<DataTransfer.Web.Donate.Payment.SaveInput>(input);

                    save.StatusID = 1;
                    save.EnteredOn = DateTime.Now;
                    save.CardNumber = Shared.Crypt.RsaEncrypt(input.CardNumber, Shared.Config.GetAppSetting("RSAPublicKey"));
                    save.Last4Digits = input.CardNumber.Substring(input.CardNumber.Length - 4);

                    output.ID = DataAccess.Web.Donate.Payment.SaveDonation(save);
                    output.IsSaved = true;
                }
            }

            return new JsonXmlResult(output);
        }

        public ActionResult ConfirmPageLoad(DataTransfer.Web.Donate.Confirm.PageLoadInput input)
        {
            var model = DataAccess.Web.Donate.Payment.ConfirmPageLoad(input);
            return new JsonXmlResult(model);
        }

        public ActionResult ConfirmDonation(DataTransfer.Web.Donate.Confirm.FormInput input)
        {
            var output = Validation.PerformBasicValidation(input);

            if (output.IsValid)
            {
                var save = Shared.ObjectHelper.CopyObject<DataTransfer.Web.Donate.Confirm.SaveInput>(input);

                save.StatusID = 2; // Pending
                DataAccess.Web.Donate.Payment.ConfirmDonation(save);
                output.IsSaved = true;
                Email.SendQuickDonationEmail(input.DonationID.ToInt());
            }

            return new JsonXmlResult(output);
        }
    }
}
